package ift615.jg;

import connect5.Grille;
import connect5.ia.GridAnalyser;
import connect5.ia.JoueurArtificiel;

public class AppRunner {

   public AppRunner() {
   }

   public static void main(String[] args) {
      AppRunner app = new AppRunner();

      try {
         app.process();
      } catch (Exception e) {
         System.out.println("Exception non geree: " + e.getMessage());
      }
   }

   private void process() {
      Grille my_grid = new Grille(6, 6);
      my_grid.reset();
      my_grid.set(0, 2, 1);// "OONNNN"
      my_grid.set(0, 3, 1);
      my_grid.set(0, 4, 1);
      my_grid.set(0, 5, 1);

      GridAnalyser anal = new GridAnalyser();
      System.out.println("Pattern NNNNOO");
      System.out.println(anal.analyseHorizontally(my_grid));

      Grille oneGrid = new Grille(6, 6);
      oneGrid.reset();

      oneGrid.set(0, 1, 1); // "ONNONN"
      oneGrid.set(0, 2, 1);
      oneGrid.set(0, 4, 1);
      oneGrid.set(0, 5, 1);
      System.out.println("Pattern ONNONN");
      System.out.println(anal.analyseHorizontally(oneGrid));

      Grille secondGrid = new Grille(6, 6);
      secondGrid.reset();

      secondGrid.set(0, 1, 1); // "ONNBNN"
      secondGrid.set(0, 2, 1);
      secondGrid.set(0, 3, 2);
      secondGrid.set(0, 4, 1);
      secondGrid.set(0, 5, 1);
      System.out.println("Pattern ONNBNN");
      System.out.println(anal.analyseHorizontally(secondGrid));

      Grille thirdGrid = new Grille(1, 15);
      thirdGrid.reset(); // OOBBONNBOOONNNN
      thirdGrid.set(0, 2, 2);
      thirdGrid.set(0, 3, 2);
      thirdGrid.set(0, 5, 1);
      thirdGrid.set(0, 6, 1);
      thirdGrid.set(0, 7, 2);
      thirdGrid.set(0, 11, 1);
      thirdGrid.set(0, 12, 1);
      thirdGrid.set(0, 13, 1);
      thirdGrid.set(0, 14, 1);

      // -1 + 100 000 + 1000 + 10 =
      System.out.println("Pattern OOBBONNBOOONNNN");
      System.out.println(anal.analyseHorizontally(thirdGrid));

      Grille fourthGrid = new Grille(5, 5);
      fourthGrid.reset();
      fourthGrid.set(0, 0, 1);
      fourthGrid.set(1, 0, 1);
      fourthGrid.set(2, 0, 1);
      fourthGrid.set(3, 0, 1);

      // 10000
      // 10000
      // 10000
      // 10000
      // 00000 = 100000

      System.out.println("Pattern colonne de 4 N");
      System.out.println(anal.analyseVertically(fourthGrid));

      Grille fifthGrid = new Grille(5, 5);
      fifthGrid.reset();
      fifthGrid.set(0, 4, 1);
      fifthGrid.set(1, 3, 1);

      // 0000N
      // 000N0
      // 00000
      // 00000
      // 00000
      System.out.println("Pattern 4 N ligne 0 et 4 N ligne 1");
      System.out.println(anal.analyseDiagonaly2(fifthGrid));

      System.out.println("Analyse d'une grille avec l'algo complet");

      Grille grid6 = new Grille(5, 5);
      grid6.reset();
      grid6.set(0, 1, 1);
      grid6.set(0, 2, 1);
      grid6.set(0, 4, 1);
      grid6.set(1, 1, 1);
      grid6.set(1, 4, 2);
      grid6.set(2, 0, 2);
      grid6.set(2, 1, 1);
      grid6.set(2, 2, 2);
      grid6.set(2, 3, 2);
      grid6.set(2, 4, 2);
      grid6.set(3, 0, 2);
      grid6.set(3, 1, 1);

      // ONNON
      // ONOOB
      // BNBBB
      // BNOOO
      // OOOOO

      double begin = System.currentTimeMillis();
      double configVal = anal.analyseGrid(grid6);
      double end = System.currentTimeMillis();

      double elapseTime = end - begin;

      System.out.println("Temps en miliseconde passe: " + elapseTime);
      System.out.println("Valeur de la config: " + configVal);

      JoueurArtificiel monJoueur = new JoueurArtificiel();

      System.out.println("le joueur noir doit jouer");
      System.out.println("choix du joueur noir: x" + monJoueur.getProchainCoup(grid6, 0)[0] + " en y: " + monJoueur.getProchainCoup(grid6, 0)[1]);
   }
}
